# ==============================================================================
# analysis-civey.R
# author: Anselm Hager
# ==============================================================================

dir <- dirname(dirname(rstudioapi::getSourceEditorContext()$path))
source(paste0(dir, "/code/setup-packages.R"))

# DATA =========================================================================

df <- read.csv(paste0(dir, "/data/civey/survey_civey.csv")) %>% 
  mutate(date = as.Date(date)) %>% 
  mutate(zip = as.character(zip)) %>% 
  mutate(zip = ifelse(str_count(zip) == 4, paste0("0",zip),zip))

vote <- read.csv(paste0(dir, "/data/votes/btw_merge.csv")) %>%
  filter(party == "afd") %>% 
  filter(date_election > "2009-09-27")

zip_ags <- read.csv(paste0(dir, "/data/crosswalks/ags_zip.csv")) %>% 
  group_by(zip) %>% 
  slice(1L)

df <- merge(df, zip_ags, by = "zip", all.x = T)

df <- df %>% 
  mutate(date_election = ifelse(date < as.Date("2017-09-24"), "2013-09-22",
                                ifelse(date < as.Date("2021-09-26"), "2017-09-24",
                                       ifelse(date > as.Date("2021-09-26"), "2021-09-26", NA))))

df$ags_date <- paste(df$ags, df$date_election) 
df <- df %>% 
  select(-date_election) %>% 
  rename(climate_balanceable_with_economy = response)
df$ags <- NULL
vote$ags_date <- paste(vote$ags, vote$date_election)

df <- merge(vote, df, by = "ags_date", all.x = T)
rm(vote)

summary(feols(climate_balanceable_with_economy ~ attacks_this_election_period + 
                age + male + density + purchasing_power + kids + 
                position_self_employed + job_edu_uni + marital_married + 
                religion_catholic + party_afd | ags + date_election, data = df))
